package com.compus.task;

import com.compus.constant.OrderStatus;
import com.compus.entity.po.Order;
import com.compus.service.OrderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

//@Component
public class OrderTask {

    @Resource
    private OrderService orderService;

    @Value("${campus.maxWait}")
    private Long maxWait;

    @Scheduled(cron = "0/5 * * * * ?")
    public void execute() {
        LocalDateTime now = LocalDateTime.now();
        orderService.lambdaUpdate()
                .eq(Order::getStatus, OrderStatus.WAIT)
                .le(Order::getCreateTime, now.minusSeconds(maxWait))
                .set(Order::getStatus, OrderStatus.CANCEL)
                .update();
    }

}
